<?php
/**
 * Created by PhpStorm.
 * User: Duke
 * Date: 2019/12/2
 * Time: 15:51
 */
namespace App\Http\Controllers\Admin;

use App\Exceptions\CoustomerException;
use App\Http\Logic\admin\LoginLogic;
use Illuminate\Http\Request;

class LoginController extends AlphaController{

    // 首页
    public function index(){
        $username = session('username');
        if($username == true) {
            return view('admin/index/index');
        }
            return view('admin/login/index');
    }
    // 表单页面展示
    public function form(){
        return view('admin/login/form');
    }
    // 登录
    public function signIn(){
        $userlogic = new LoginLogic();
        $i = json_decode($_POST['data'],true);
        $post = [
            'username' => $i['username'],
            'password' => md5($i['password'])
        ];
        $categoryInfo = $userlogic->signInLogic($post);
        if($categoryInfo){
            $ret = ['code'=>0,'msg'=>'登陆成功'];
        }else{
            $ret = ['code'=>1001,'msg'=>'登录失败，密码或账号错误，请重新输入！'];
        }
        return json_encode($ret);
    }
    // 退出
    public function signOut(Request $request){
        $request->session()->forget('username');
        $ret = ['code'=>0,'msg'=>'退出成功'];
        return json_encode($ret);
    }
    // 注册
    public function register(){
        $userlogic = new LoginLogic();
        $i = json_decode($_POST['data'],true);
        $post = [
            'username' => $i['username'],
            'password' => md5($i['password']),
        ];
        $categoryInfo = $userlogic->registerLogic($post);
        if($categoryInfo){
            $ret = ['code'=>0,'msg'=>'恭喜你，注册成功'];
        }
        return json_encode($ret);
    }
}
